package main

import (
	"fmt"
	"my/go_draft/redis/config"
	"time"

	"github.com/garyburd/redigo/redis"
)

func main() {
	defer runPanic()
	pool := initPool()
	conn := pool.Get()
	conn.Do("set", "name", "hwanginsitein111")
	name, _ := redis.String(conn.Do("get", "name"))
	fmt.Println(name)
}

func runPanic() {
	if err := recover(); err != nil {
		fmt.Println(111)
	}
}
func initPool() *redis.Pool {
	pool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			c, _ := redis.Dial(config.ConnectType, config.IP+":"+config.Port)
			_, authErr := c.Do("auth", config.RequirePass)
			if authErr != nil {
				c.Close()
				return nil, authErr
			}
			_, selectDbErr := c.Do("select", 0)
			if selectDbErr != nil {
				c.Close()
				return nil, selectDbErr
			}
			return c, nil
		},
		MaxIdle:     8,
		MaxActive:   0,
		IdleTimeout: time.Nanosecond * 100,
	}
	return pool
}
